<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="generator" content="VuePress 2.0.0-beta.48">
    <script>
      (function() {
        const userMode = localStorage.getItem('vuepress-reco-color-scheme') || 'auto';
        const systemDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

        if (userMode === 'dark' || (userMode === 'auto' && systemDarkMode)) {
          document.documentElement.classList.toggle('dark', true);
        }
      })();
    </script>
    <title>pyquery解析库 | 想走过亚洲的熊</title><meta name="description" content="Just playing around">
    <link rel="modulepreload" href="/assets/app.df912e7f.js"><link rel="modulepreload" href="/assets/pyquery.html.9b4664bc.js"><link rel="modulepreload" href="/assets/pyquery.html.b3735f9e.js"><link rel="prefetch" href="/assets/index.html.7b08e917.js"><link rel="prefetch" href="/assets/guide.html.f296697c.js"><link rel="prefetch" href="/assets/ha.html.14d13ae5.js"><link rel="prefetch" href="/assets/indexd.html.196743f0.js"><link rel="prefetch" href="/assets/api.html.95a8c775.js"><link rel="prefetch" href="/assets/home.html.36abf5d3.js"><link rel="prefetch" href="/assets/plugin.html.be5c0441.js"><link rel="prefetch" href="/assets/pyquery.html.75e4a85f.js"><link rel="prefetch" href="/assets/theme.html.6665517d.js"><link rel="prefetch" href="/assets/haha.html.9d103d7e.js"><link rel="prefetch" href="/assets/404.html.d341cb57.js"><link rel="prefetch" href="/assets/index.html.dab3a2d2.js"><link rel="prefetch" href="/assets/index.html.46ca51f1.js"><link rel="prefetch" href="/assets/index.html.72ac4063.js"><link rel="prefetch" href="/assets/index.html.b57762aa.js"><link rel="prefetch" href="/assets/index.html.038ed3b5.js"><link rel="prefetch" href="/assets/guide.html.9abc2005.js"><link rel="prefetch" href="/assets/ha.html.e982005d.js"><link rel="prefetch" href="/assets/indexd.html.f4b65589.js"><link rel="prefetch" href="/assets/api.html.4573e839.js"><link rel="prefetch" href="/assets/home.html.50633c01.js"><link rel="prefetch" href="/assets/plugin.html.0a7eeba6.js"><link rel="prefetch" href="/assets/pyquery.html.219f266c.js"><link rel="prefetch" href="/assets/theme.html.04d9f7d6.js"><link rel="prefetch" href="/assets/haha.html.292c0b98.js"><link rel="prefetch" href="/assets/404.html.58df5a57.js"><link rel="prefetch" href="/assets/index.html.b854e198.js"><link rel="prefetch" href="/assets/index.html.75528c67.js"><link rel="prefetch" href="/assets/index.html.c3133bbd.js"><link rel="prefetch" href="/assets/index.html.60410340.js"><link rel="prefetch" href="/assets/404.281ffc9d.js"><link rel="prefetch" href="/assets/Categories.a904d6b5.js"><link rel="prefetch" href="/assets/Layout.e97e47b2.js"><link rel="prefetch" href="/assets/Post.73142944.js"><link rel="prefetch" href="/assets/Timeline.af9347e6.js"><link rel="prefetch" href="/assets/reco-valine.88224ab5.js">
    <link rel="stylesheet" href="/assets/style.0984aebf.css">
  </head>
  <body>
    <div id="app"><!--[--><div class="theme-container"><div class="common-wrapper"><div><header class="navbar-container"><div class="toggle-sidebar-button" aria-expanded="false" role="button" tabindex="0"><div class="icon" aria-hidden="true"><span></span><span></span><span></span></div></div><span class="site-brand"><a href="/" class=""><img class="logo" src="/logo.jpg" alt="想走过亚洲的熊"><span class="site-name can-hide">想走过亚洲的熊</span></a></span><div class="navbar-links-wrapper" style=""><form class="search-box" role="search"><input type="search" autocomplete="off" spellcheck="false" value><!----></form><nav class="navbar-links"><!--[--><div class="navbar-links-item"><a href="/docs/guide" class="nav-link" aria-label="指南"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->指南<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/categories/html5/1/" class="nav-link" aria-label="分类"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->分类<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/tags/html5/1/" class="nav-link" aria-label="标签"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->标签<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="HTML5"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->HTML5<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="HTML5"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->HTML5<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/html5/indexd" class="nav-link" aria-label="第一"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->第一<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/html5/haha.md" class="nav-link" aria-label="哈哈链接"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->哈哈链接<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="CSS3"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->CSS3<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="CSS3"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->CSS3<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="JavaScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->JavaScript<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="JavaScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->JavaScript<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="TypeScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->TypeScript<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="TypeScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->TypeScript<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="Vue"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Vue<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="Vue"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Vue<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="React"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->React<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="React"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->React<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="Python"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Python<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="Python"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Python<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="算法"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->算法<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="算法"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->算法<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><!--]--></nav><a class="icon-container left btn-toggle-dark-mode" href="javascript:void(0)"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewbox="0 0 24 24" style="color:inherit;width:20px;height:20px;font-size:20px;"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 3h.393a7.5 7.5 0 0 0 7.92 12.446A9 9 0 1 1 12 2.992z"></path><path d="M17 4a2 2 0 0 0 2 2a2 2 0 0 0-2 2a2 2 0 0 0-2-2a2 2 0 0 0 2-2"></path><path d="M19 11h2m-1-1v2"></path></g></svg><!----></a></div></header><div class="sidebar-mask"></div><aside class="series-container"><nav class="navbar-links"><!--[--><div class="navbar-links-item"><a href="/docs/guide" class="nav-link" aria-label="指南"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->指南<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/categories/html5/1/" class="nav-link" aria-label="分类"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->分类<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><a href="/tags/html5/1/" class="nav-link" aria-label="标签"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->标签<!--]--></span></a><!--[--><!--]--></a></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="HTML5"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->HTML5<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="HTML5"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->HTML5<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/html5/indexd" class="nav-link" aria-label="第一"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->第一<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/html5/haha.md" class="nav-link" aria-label="哈哈链接"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->哈哈链接<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="CSS3"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->CSS3<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="CSS3"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->CSS3<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="JavaScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->JavaScript<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="JavaScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->JavaScript<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="TypeScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->TypeScript<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="TypeScript"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->TypeScript<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="Vue"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Vue<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="Vue"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Vue<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="React"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->React<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="React"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->React<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="Python"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Python<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="Python"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->Python<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><div class="navbar-links-item"><div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="算法"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->算法<!--]--></span></a><span class="arrow down"></span></button><button class="mobile-dropdown-title" type="button" aria-label="算法"><a class="icon-container left title" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->算法<!--]--></span></a><span class="right arrow"></span></button><ul style="display:none;" class="nav-dropdown"><!--[--><li class="dropdown-item"><a href="/docs/theme-reco/theme" class="nav-link" aria-label="vuepress-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-reco<!--]--></span></a><!--[--><!--]--></a></li><li class="dropdown-item"><a href="/blogs/other/guide" class="nav-link" aria-label="vuepress-theme-reco"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->vuepress-theme-reco<!--]--></span></a><!--[--><!--]--></a></li><!--]--></ul></div></div><!--]--></nav><!--[--><!--[--><section class="series-group series-item"><h5 class="series-heading active">文章导航</h5><ul><li><!--[--><a href="/docs/html5/indexd.html" class="nav-link series-item" aria-label="好"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->好<!--]--></span></a><!--[--><!--]--></a><!--]--></li><li><!--[--><a href="/docs/html5/ha.html" class="nav-link series-item" aria-label="hah"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->hah<!--]--></span></a><!--[--><!--]--></a><!--]--></li><li><!--[--><a aria-current="page" href="/docs/html5/pyquery.html" class="router-link-active router-link-exact-active nav-link router-link-active series-item active" aria-label="pyquery解析库"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->pyquery解析库<!--]--></span></a><!--[--><!--]--></a><!--]--></li></ul></section><!--]--><!--]--></aside><!--[--><main class="page-container show-series show-catalog"><h1 class="page-title">pyquery解析库</h1><div class="page-info"><a class="icon-container left" href="javascript:void(0)"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewbox="0 0 24 24" style="color:inherit;width:18px;height:18px;font-size:18px;"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="7" r="4"></circle><path d="M6 21v-2a4 4 0 0 1 4-4h4a4 4 0 0 1 4 4v2"></path></g></svg><span style="color:inherit;font-size:14px;"><!--[-->想走过亚洲的熊<!--]--></span></a><!----><!----><!----><!----></div><div class="theme-reco-default-content"><div><h1 id="pyquery解析库" tabindex="-1"><a class="header-anchor" href="#pyquery解析库" aria-hidden="true">#</a> pyquery解析库</h1><h2 id="_1-什么是pyquery" tabindex="-1"><a class="header-anchor" href="#_1-什么是pyquery" aria-hidden="true">#</a> 1.什么是pyquery</h2><p>pyquery是类似于jquery的网页解析工具，让你使用jquery的风格来遍历xml文档，它使用lxml操作html的xml文档，它的语法与jquery很像，和我们之前所讲的解析库xpath与BeautifulSoup比起来更加灵活与简便，并且增加了添加类和移除节点的操作，这些操作有时会为提取信息时带来极大的便利。</p><h2 id="_2-使用pyquery" tabindex="-1"><a class="header-anchor" href="#_2-使用pyquery" aria-hidden="true">#</a> 2.使用pyquery</h2><p>如果你对web有所了解，并且比较喜欢使用CSS选择器，那么这里有一款更适合你的解析库——jquery。</p><h3 id="_2-1-准备工作" tabindex="-1"><a class="header-anchor" href="#_2-1-准备工作" aria-hidden="true">#</a> 2.1 准备工作</h3><p>在使用之前，请确保已经安装好qyquery库。安装教程如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>pip install pyquery
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h3 id="_2-2-初始化" tabindex="-1"><a class="header-anchor" href="#_2-2-初始化" aria-hidden="true">#</a> 2.2 初始化</h3><p>和Beautiul Soup一样，在初始化pyquery的时候，也需要传入html文本来初始化一个pyquery对象。</p><p>初始化的时候一般有三种传入方式：传入字符串、传入URL、传入html文件。</p><ul><li><strong>字符串初始化</strong></li></ul><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
    &lt;ul&gt;
        &lt;li class=&quot;item-0&quot;&gt;张三&lt;/li&gt;
        &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;李四&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot; bold&gt;王五&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;赵六&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;王麻子&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>先对上面的代码做简单的描述：</p><p>首先引入PyQuery对象，取名为pq。然后声明一个长HTML字符串，并将其当作参数传给PyQuery类，这样就成功的进行了初始化。</p><p>接下来将css选择器作为参数传入初始化对象，在这个示例中我们传入<code>li</code>节点，这样就可以选择所有的<code>li</code>节点.。</p><ul><li><strong>URL初始化</strong></li></ul><p>初始化对象的参数不仅可以是字符串，还可以是网页的URL，这时可以将URL作为参数传入初始化对象。</p><p>具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span><span class="token string">&#39;http://www.baidu.com&#39;</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;title&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;title&gt;百度一下，你就知道&lt;/title&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>试着运行上面的代码你会发现，我们成功的获取到了百度的<code>title</code>节点和网页信息。</p><p>PyQuery对象会先请求这个URL，然后用得到的HTML内容完成初始化，这其实就相当于网页源代码以字符串的形式传递给初始化对象。</p><p>因此，还可以这样写代码：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq
<span class="token keyword">import</span> requests


url <span class="token operator">=</span> <span class="token string">&#39;https://www.baidu.com&#39;</span>
doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">.</span>content<span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">&#39;utf-8&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>doc<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;title&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行结果与上面那段代码的运行结果是一致的。</p><ul><li><strong>文件初始化</strong></li></ul><p>除了传递URL以外还可以传递本地的文件名，此时只要传递本地文件名，此时将参数指定为filename即可。</p><p>具体代码如下所示：</p><p><code>pyquery.html</code></p><div class="language-html ext-html line-numbers-mode"><pre class="language-html"><code><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>en<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>UTF-8<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">&gt;</span></span>Title<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>张三<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-1<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link2.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>李四<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0 active<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link3.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span> <span class="token attr-name">bold</span><span class="token punctuation">&gt;</span></span>王五<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-1 active<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link4.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>赵六<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link5.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>王麻子<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">&gt;</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

<span class="token keyword">with</span> <span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">&quot;./pyquery.html&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;r&quot;</span><span class="token punctuation">,</span> encoding<span class="token operator">=</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">)</span><span class="token keyword">as</span> f<span class="token punctuation">:</span>
    content <span class="token operator">=</span> f<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span>

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>content<span class="token punctuation">)</span>
<span class="token comment"># print(doc)</span>
<span class="token comment"># print(type(doc)) #&lt;class &#39;pyquery.pyquery.PyQuery&#39;&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>以上三种初始化的方式都是可以的，当然最常用的初始化方式还是以字符串的形式传递。</p><h3 id="_2-3-基本css选择器" tabindex="-1"><a class="header-anchor" href="#_2-3-基本css选择器" aria-hidden="true">#</a> 2.3 基本CSS选择器</h3><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
    &lt;ul id=&quot;container&quot;&gt;
        &lt;li class=&quot;item-0 list&quot;&gt;张三&lt;/li&gt;
        &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;李四&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;bold&quot;&gt;王五&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;赵六&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;王麻子&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;#container .list&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;li class=&quot;item-0 list&quot;&gt;张三&lt;/li&gt;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>doc<span class="token punctuation">(</span><span class="token string">&#39;#container .list&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment"># &lt;class &#39;pyquery.pyquery.PyQuery&#39;&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>初始化PyQuery对象之后，传入CSS选择器<code>#container .list li</code>将所有符合条件的节点输出，并且运行上面的代码之后你会发现它的类型依然还是PyQuery类型。</p><h3 id="_2-4-查找节点" tabindex="-1"><a class="header-anchor" href="#_2-4-查找节点" aria-hidden="true">#</a> 2.4 查找节点</h3><p>下面介绍一些常用的查询函数，这些函数与jQuery函数的用法是完全相同的。</p><ul><li><strong>子节点</strong></li></ul><p>查找子节点时需要用到<code>find()</code>方法，并传入的参数是CSS选择器，以前面的html为例子。</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>doc<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>doc<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>调用<code>find()</code>方法，将节点名称<code>li</code>传入该方法，获取所有符合条件的内容。类型依然还是PyQuery。</p><p>当然我们还可以这样写：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list&#39;</span><span class="token punctuation">)</span>

<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>items<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#&lt;class &#39;pyquery.pyquery.PyQuery&#39;&gt;</span>

lis <span class="token operator">=</span> items<span class="token punctuation">.</span>find<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#&lt;class &#39;pyquery.pyquery.PyQuery&#39;&gt;</span>

<span class="token keyword">print</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>首先先选取class为<code>list</code>的节点，然后调用<code>find()</code>方法，传入CSS选择器，选取内部的<code>li</code>节点，最后打印输出。</p><p>其实<code>find()</code>方法是查找所有的子孙节点，要获取所有的<strong>子节点</strong>可以调用<code>chirdren()</code>方法。具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list&#39;</span><span class="token punctuation">)</span>
lis <span class="token operator">=</span> items<span class="token punctuation">.</span>children<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果想要筛选子节点中符合条件的节点，可以向<code>chirdren()</code>方法传入CSS选择器。具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list&#39;</span><span class="token punctuation">)</span>
lis <span class="token operator">=</span> items<span class="token punctuation">.</span>children<span class="token punctuation">(</span><span class="token string">&#39;.active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#&lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;bold&quot;&gt;王五&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;</span>
<span class="token comment">#&lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;赵六&lt;/a&gt;&lt;/li&gt;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>试着运行上面的代码你会发现，这里已经成功获取到了class为<code>active</code>的节点。</p><ul><li><strong>父节点</strong></li></ul><p>我们可以调用<code>parent()</code>方法来获取某个节点的父节点。</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;container&quot;&gt;
    &lt;ul class=&quot;list&quot;&gt;
        &lt;li class=&quot;item-0&quot;&gt;张三&lt;/li&gt;
        &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;李四&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;bold&quot;&gt;王五&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;赵六&lt;/a&gt;&lt;/li&gt;
        &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;王麻子&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list&#39;</span><span class="token punctuation">)</span>
container <span class="token operator">=</span> items<span class="token punctuation">.</span>parent<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>container<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>container<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>先对上面的代码做简要的说明：</p><p>首先选取class为<code>list</code>的节点，然后再调用<code>parent()</code>方法得到其父节点，其类型依然还是PyQuery类型。</p><p>这里的父节点是直接父节点，但是如果要获取祖父节点，可以调用<code>parents()</code>方法。</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item=-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;

&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list&#39;</span><span class="token punctuation">)</span>
container <span class="token operator">=</span> items<span class="token punctuation">.</span>parents<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>container<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>container<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行上面的代应为码之后，你会发现这里输出的内容有四个，因为class为<code>list</code>节点的祖父节点有四个，分别是：container、wrap、body、html。在初始化对象的时候已经添加上了body和html节点。</p><ul><li><strong>兄弟节点</strong></li></ul><p>除了可以获取到父节点和子节点之外，还可以获取到兄弟节点。如果需要获取兄弟节点，可以调用<code>siblings()</code>方法。</p><p>具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list .item-0.active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>items<span class="token punctuation">.</span>siblings<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>  <span class="token comment">#过滤掉 third item </span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>这里首先选取类为<code>.item-0.active</code>的节点，再调用<code>siblings()</code>方法获取到该节点的兄弟节点。</p><p>试着运行上面的代码，你会发现获取到其他四个兄弟节点。</p><h3 id="_2-5-遍历" tabindex="-1"><a class="header-anchor" href="#_2-5-遍历" aria-hidden="true">#</a> 2.5 遍历</h3><p>通过上面的代码可以观察到，pyquery的选择结果可能是多个节点，也可能是单个节点，类型都是PyQuery类型，并没有向BeautifulSoup那样的列表。</p><p>对于单个节点来说，可以直接打印输出，也可以直接转成字符串。</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list .item-0.active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>items<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span>items<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token builtin">type</span><span class="token punctuation">(</span>items<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于多个节点，可以通过调用<code>item()</code>方法，将获取的内容转换成生成器类型，在通过遍历的方式输出。</p><p>具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
lis <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>lis<span class="token punctuation">)</span>  <span class="token comment"># &lt;generator object PyQuery.items at 0x000001E2647D86D0&gt;</span>

<span class="token keyword">for</span> li <span class="token keyword">in</span> lis<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行上面的代码，你会发现输出变量<code>lis</code>的结果是生成器，因此可以遍历输出。</p><h3 id="_2-6-获取信息" tabindex="-1"><a class="header-anchor" href="#_2-6-获取信息" aria-hidden="true">#</a> 2.6 获取信息</h3><p>一般来说，在网页里面我们需要获取的信息有两类：一类是文本内容，另一类是节点属性值。</p><ul><li><strong>获取属性</strong></li></ul><p>获取到某个PyQuery类型的节点之后，就可以通过<code>attr()</code>方法来获取属性。</p><p>具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
a <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list .item-0.active a&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>attr<span class="token punctuation">(</span><span class="token string">&#39;href&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#link3.html</span>

</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>先获取class为<code>list</code>下面的class为<code>item-0 active</code>的节点下的<code>a</code>节点，这时变量<code>a</code>是PyQuery类型，再调用<code>attr()</code>方法并传入属性值<code>href</code>。</p><p>当然，我们也可以获取到<strong>所有<code>a</code>节点的属性</strong>，具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
a <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;a&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> a<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>attr<span class="token punctuation">(</span><span class="token string">&#39;href&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#输出结果</span>
link2<span class="token punctuation">.</span>html
link3<span class="token punctuation">.</span>html
link4<span class="token punctuation">.</span>html
link5<span class="token punctuation">.</span>html
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>但是如果代码这样写：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
a <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;a&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>attr<span class="token punctuation">(</span><span class="token string">&#39;href&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#link2.html</span>

</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行上面的代码之后，你会发现只获取到第一个<code>a</code>节点的<code>href</code>属性。</p><p>所有这个是需要注意的地方！！</p><ul><li><strong>提取文本</strong></li></ul><p>提取文本与提取属性的逻辑是一样的，首先获取到class为PyQuery的节点，再调用<code>text()</code>方法获取文本。</p><p>首先来获取一个节点的文本内容。具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
a <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list .item-0.active a&#39;</span><span class="token punctuation">)</span> <span class="token comment">#third item</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>a<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>试着运行上面的代码你会发现成功获取<code>a</code>节点的文本内容。</p><p>接下来我们就来获取多个<code>li</code>节点的文本内容。</p><p>具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>items<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#输出</span>
first<span class="token operator">-</span>item second item third item fourth item fifth item
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行上面的代码，你会发现该代码成功获取到了所有节点名称为<code>li</code>的文本内容，中间用空格隔开。</p><p>如果你想要一个一个获取，那还是少不了生成器，具体代码如下所示：</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;
&lt;/div&gt;
&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq

doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
items <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> items<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    
<span class="token comment"># 输出</span>
<span class="token triple-quoted-string string">&#39;&#39;&#39;
first-item
second item
third item
fourth item
fifth item
&#39;&#39;&#39;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_2-7-节点操作" tabindex="-1"><a class="header-anchor" href="#_2-7-节点操作" aria-hidden="true">#</a> 2.7 节点操作</h3><p>pyquery提供了一系列方法对节点进行动态修改，比如为某个节点<strong>添加</strong>一个class，<strong>移除</strong>某个节点，这些操作有时会为提取信息带来便利。</p><ul><li><strong>add_class和remove_class</strong></li></ul><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;

&lt;/div&gt;

&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.list .item-0.active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li<span class="token punctuation">.</span>remove_class<span class="token punctuation">(</span><span class="token string">&#39;active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li<span class="token punctuation">.</span>add_class<span class="token punctuation">(</span><span class="token string">&#39;active&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>运行结果如下所示：</p><div class="language-xml ext-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0 active<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link3.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span> <span class="token attr-name">bold</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>third item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
            
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link3.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span> <span class="token attr-name">bold</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>third item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
            
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item-0 active<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>link3.html<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>span</span> <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span> <span class="token attr-name">bold</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span><span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>third item<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>span</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>a</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>上面有三段输出内容，首先先获取一个<code>li</code>节点，然后再删除<code>active</code>类属性，第三段代码是添加<code>active</code>类属性。</p><h3 id="_2-8-伪类选择器" tabindex="-1"><a class="header-anchor" href="#_2-8-伪类选择器" aria-hidden="true">#</a> 2.8 伪类选择器</h3><p>CSS选择器之所以强大，还有一个很重要的原因，那就是它可以支持多种多样的伪类选择器，例如选择第一个节点、最后一个节点、奇偶数节点、包含某一文本的节点。</p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code>html <span class="token operator">=</span> <span class="token triple-quoted-string string">&#39;&#39;&#39;
&lt;div class=&quot;wrap&quot;&gt;
    &lt;div id=&quot;container&quot;&gt;
        &lt;ul class=&quot;list&quot;&gt;
            &lt;li class=&quot;item-0&quot;&gt;first-item&lt;/li&gt;
            &lt;li class=&quot;item-1&quot;&gt;&lt;a href=&quot;link2.html&quot;&gt;second item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0 active&quot;&gt;&lt;a href=&quot;link3.html&quot;&gt;&lt;span class=&quot;&quot;bold&gt;third item&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-1 active&quot;&gt;&lt;a href=&quot;link4.html&quot;&gt;fourth item&lt;/a&gt;&lt;/li&gt;
            &lt;li class=&quot;item-0&quot;&gt;&lt;a href=&quot;link5.html&quot;&gt;fifth item&lt;/a&gt;&lt;/li&gt;        
        &lt;/ul&gt;
    &lt;/div&gt;

&lt;/div&gt;

&#39;&#39;&#39;</span>

<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq


doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>html<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:first-child&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 第一个li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:last-child&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 最后一个li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:nth-child(2)&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 第二个位置的li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:gt(2)&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 第三个之后的li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:nth-child(2n)&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 偶数位置的li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
li <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;li:contains(second)&#39;</span><span class="token punctuation">)</span>	<span class="token comment"># 包含second文本的li节点</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>li<span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>至此，关于pyquery的所有内容都讲完了，接下来就进入实战了，光说不练肯定是不行的，只有通过实战才能正真学会刚刚所学会的知识。</p><h2 id="_3-实战" tabindex="-1"><a class="header-anchor" href="#_3-实战" aria-hidden="true">#</a> 3.实战</h2><p><strong>抓取B站排行榜数据</strong></p><div class="language-python ext-py line-numbers-mode"><pre class="language-python"><code><span class="token comment"># -*- coding: UTF-8 -*-</span>
<span class="token keyword">from</span> pyquery <span class="token keyword">import</span> PyQuery <span class="token keyword">as</span> pq
<span class="token keyword">import</span> requests
<span class="token keyword">import</span> logging
<span class="token keyword">import</span> openpyxl

wb <span class="token operator">=</span> openpyxl<span class="token punctuation">.</span>Workbook<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 初始化工作簿对象</span>
sheet <span class="token operator">=</span> wb<span class="token punctuation">.</span>active  <span class="token comment"># 获取活动的工作表</span>
<span class="token comment"># 添加列名</span>
sheet<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">&#39;rank&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;title&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;link&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;bicon_play&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;bicon_view&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;creator&#39;</span><span class="token punctuation">,</span> <span class="token string">&#39;score&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token comment"># 日志输出配置</span>
logging<span class="token punctuation">.</span>basicConfig<span class="token punctuation">(</span>level<span class="token operator">=</span>logging<span class="token punctuation">.</span>INFO<span class="token punctuation">,</span> <span class="token builtin">format</span><span class="token operator">=</span><span class="token string">&#39;%(asctime)s - %(levelname)s: %(message)s&#39;</span><span class="token punctuation">)</span>
<span class="token comment"># 伪装请求头</span>
headers <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token string">&quot;Origin&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;https://www.bilibili.com&quot;</span><span class="token punctuation">,</span>
    <span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1&quot;</span>
<span class="token punctuation">}</span>
<span class="token comment"># 目标URL</span>
url <span class="token operator">=</span> <span class="token string">&#39;https://www.bilibili.com/v/popular/rank/all&#39;</span>
<span class="token comment"># request请求获取的文本传入PyQuery初始化</span>
resp <span class="token operator">=</span> requests<span class="token punctuation">.</span>get<span class="token punctuation">(</span>url<span class="token punctuation">,</span> headers<span class="token operator">=</span>headers<span class="token punctuation">)</span><span class="token punctuation">.</span>text
doc <span class="token operator">=</span> pq<span class="token punctuation">(</span>resp<span class="token punctuation">)</span>
<span class="token comment"># 获取class=rank-list 下所有li节点内容</span>
<span class="token comment"># 遍历li节点</span>
con1 <span class="token operator">=</span> doc<span class="token punctuation">(</span><span class="token string">&#39;.rank-list li&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">for</span> item <span class="token keyword">in</span> con1<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    rank <span class="token operator">=</span> item<span class="token punctuation">(</span><span class="token string">&#39;.num&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 排名</span>
    title <span class="token operator">=</span> item<span class="token punctuation">(</span><span class="token string">&#39;.content .info a:first-child&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 视频标题</span>
    link <span class="token operator">=</span> <span class="token string">&#39;https:&#39;</span> <span class="token operator">+</span> item<span class="token punctuation">(</span><span class="token string">&#39;.content .info a&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>attr<span class="token punctuation">(</span><span class="token string">&#39;href&#39;</span><span class="token punctuation">)</span>  <span class="token comment"># 视频链接</span>
    <span class="token comment"># creator = item(&#39;.content .info .detail a span&#39;).text()        # UP主</span>
    bicon_play<span class="token punctuation">,</span> bicon_view<span class="token punctuation">,</span> creator <span class="token operator">=</span> item<span class="token punctuation">(</span><span class="token string">&#39;.content .info .detail span&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>split<span class="token punctuation">(</span><span class="token string">&#39; &#39;</span><span class="token punctuation">)</span>
    <span class="token comment"># print(bicon_play, bicon_view, creator)     # 排名</span>
    score <span class="token operator">=</span> item<span class="token punctuation">(</span><span class="token string">&#39;.content .info .pts div&#39;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>text<span class="token punctuation">(</span><span class="token punctuation">)</span>
    sheet<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">[</span>rank<span class="token punctuation">,</span> title<span class="token punctuation">,</span> link<span class="token punctuation">,</span> bicon_play<span class="token punctuation">,</span> bicon_view<span class="token punctuation">,</span> creator<span class="token punctuation">,</span> score<span class="token punctuation">]</span><span class="token punctuation">)</span>
    logging<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token punctuation">[</span>rank<span class="token punctuation">,</span> title<span class="token punctuation">,</span> link<span class="token punctuation">,</span> bicon_play<span class="token punctuation">,</span> bicon_view<span class="token punctuation">,</span> creator<span class="token punctuation">,</span> score<span class="token punctuation">]</span><span class="token punctuation">)</span>

wb<span class="token punctuation">.</span>save<span class="token punctuation">(</span>filename<span class="token operator">=</span><span class="token string">&#39;data.xlsx&#39;</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></div><footer class="page-meta"><div class="meta-item edit-link"><a class="nav-link meta-item-label" href="https://github.com/vuepress-reco/vuepress-theme-reco-next/edit/main/example//docs/html5/pyquery.md" rel="noopener noreferrer" target="_blank" aria-label="Edit this page"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewbox="0 0 24 24" style="color:inherit;width:20px;height:20px;font-size:20px;"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M9 7H6a2 2 0 0 0-2 2v9a2 2 0 0 0 2 2h9a2 2 0 0 0 2-2v-3"></path><path d="M9 15h3l8.5-8.5a1.5 1.5 0 0 0-3-3L9 12v3"></path><path d="M16 5l3 3"></path></g></svg><span style="color:inherit;font-size:14px;"><!--[-->Edit this page<!--]--></span></a><!----><!--[--><!--]--></a></div><!----></footer><nav class="page-nav"><p class="hasPrev inner"><span class="page-nav-item prev"> ← hah</span><!----></p></nav><!----></main><!--]--><div class="page-catalog-container"><h5 class="tip">标题目录</h5><ul><!--[--><!--[--><li class="page-catalog-menu-depth_2"><a aria-current="page" href="/docs/html5/pyquery.html#_1-什么是pyquery" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="1.什么是pyquery"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->1.什么是pyquery<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_2"><a aria-current="page" href="/docs/html5/pyquery.html#_2-使用pyquery" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.使用pyquery"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.使用pyquery<!--]--></span></a><!--[--><!--]--></a></li><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-1-准备工作" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.1 准备工作"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.1 准备工作<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-2-初始化" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.2 初始化"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.2 初始化<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-3-基本css选择器" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.3 基本CSS选择器"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.3 基本CSS选择器<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-4-查找节点" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.4 查找节点"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.4 查找节点<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-5-遍历" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.5 遍历"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.5 遍历<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-6-获取信息" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.6 获取信息"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.6 获取信息<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-7-节点操作" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.7 节点操作"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.7 节点操作<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--[--><li class="page-catalog-menu-depth_3"><a aria-current="page" href="/docs/html5/pyquery.html#_2-8-伪类选择器" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="2.8 伪类选择器"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->2.8 伪类选择器<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--]--><!--[--><li class="page-catalog-menu-depth_2"><a aria-current="page" href="/docs/html5/pyquery.html#_3-实战" class="router-link-active router-link-exact-active nav-link page-catalog-item" aria-label="3.实战"><!--[--><!--]--><a class="icon-container left" href="javascript:void(0)"><!----><span style="color:inherit;font-size:14px;"><!--[-->3.实战<!--]--></span></a><!--[--><!--]--></a></li><!--]--><!--]--></ul></div></div></div></div><!----><!----><!--]--></div>
    <script type="module" src="/assets/app.df912e7f.js" defer></script>
  </body>
</html>
